/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbCloud			*/
/*	Author:		gong_libin			*/
/*	Date:		2012_01_18			*/
/*	File:		MrMsg.cpp			*/
/********************************************************/

#include "MrMsg.h"

CMrMsg::CMrMsg()
{
	m_iMsg = MR_FAILURE;
}

CMrMsg::~CMrMsg()
{
}

void CMrMsg::MrIpcDelete()
{
	if (MR_FAILURE != m_iMsg) {
		msgctl(m_iMsg, IPC_RMID, 0);
	}

	return;
}

void CMrMsg::MrIpcSetKernel(ULONG ulMax, ULONG ulMnb, ULONG ulMni)
{
	char szCmd[MR_PACKET] = { 0 };

	sprintf(szCmd, "sysctl -w kernel.msgmax=%ld", ulMax);
	system(szCmd);

	memset(szCmd, '\0', MR_PACKET);
	sprintf(szCmd, "sysctl -w kernel.msgmnb=%ld", ulMnb);
	system(szCmd);

	memset(szCmd, '\0', MR_PACKET);
	sprintf(szCmd, "sysctl -w kernel.msgmni=%ld", ulMni);
	system(szCmd);

	return;
}

int CMrMsg::MrIpcCreate(key_t iKey)
{
	m_iMsg = msgget(iKey, IPC_CREAT | 0777);
	if (MR_FAILURE == m_iMsg) {
		MR_ERROR("%s\n", strerror(errno));
		return MR_FAILURE;
	}

	return MR_SUCCESS;
}

int CMrMsg::MrMsgPut(void* pMsg, UINT uiSize)
{
	if (MR_SUCCESS != msgsnd(m_iMsg, pMsg, uiSize, 0)) {
		MR_ERROR("%s\n", strerror(errno));
		return MR_FAILURE;
	}

	return MR_SUCCESS;
}

int CMrMsg::MrMsgGet(void* pMsg, UINT uiSize)
{
	if (MR_FAILURE ==  msgrcv(m_iMsg, pMsg, uiSize, 0, 0)) {
		MR_ERROR("%s\n", strerror(errno));
		return MR_FAILURE;
	}

	return MR_SUCCESS;
}
